package com.gzz.controller;


import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gzz.common.R;
import com.gzz.pojo.BaseInfo;
import com.gzz.pojo.Cart;
import com.gzz.pojo.GoodsInfo;
import com.gzz.service.BaseInfoService;
import com.gzz.service.BasePositionService;
import com.gzz.service.GoodsInfoService;
import com.gzz.utils.JWTUtils;
import com.gzz.vo.CartInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static jdk.nashorn.internal.objects.NativeMath.round;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周佳成
 * @since 2023-11-24
 */
@RestController
@Slf4j
@RequestMapping("/api/base-info")
public class BaseInfoController {
    @Autowired
    BasePositionService basePositionService;
    @Autowired
    BaseInfoService baseInfoService;
    @Autowired
    GoodsInfoService goodsInfoService;
     @PostMapping("/add")
    public R<List<String>> orderAdd(HttpServletRequest request, @RequestBody List<CartInfo> cartList)
     {
         log.info("进行中");
         String token = request.getHeader("token");
         Long id = JWTUtils.parseToken(token);
          List<BaseInfo> baseInfoList = new ArrayList<>();
          List<String> orderIdList = new ArrayList<>();
         System.out.println(cartList.get(0).getCartId()+"我的谓词");
          for(CartInfo cart : cartList)
          {

              String orderId = UUID.randomUUID().toString();
              BaseInfo baseInfo = new BaseInfo();
              baseInfo.setUserId(id);
              GoodsInfo goodsInfo = goodsInfoService.getById(cart.getGoodsId());
              double price = round(goodsInfo.getGoodsPrice().doubleValue()*cart.getGoodsNum(),2);
              baseInfo.setGoodsNum((long)cart.getGoodsNum());
              baseInfo.setOrderTime(DateTime.now());
              baseInfo.setOrderId(orderId);
              baseInfo.setSumPrice(price);
              baseInfo.setTab(1);
              baseInfo.setGoodsName(goodsInfo.getGoodsName());
              baseInfoList.add(baseInfo);
              orderIdList.add(orderId);
          }
          baseInfoService.saveBatch(baseInfoList);

         log.info("内容为:"+cartList.get(0).toString());
              return R.success(orderIdList);
     }
     @PutMapping("/change")
    public R changeState(@RequestBody List<String> stringList)
     {
         UpdateWrapper<BaseInfo> baseInfoUpdateWrapper = new UpdateWrapper<>();
         baseInfoUpdateWrapper.in("order_Id",stringList);
         baseInfoUpdateWrapper.set("tab",2);
         baseInfoService.update(baseInfoUpdateWrapper);
         return R.success(true);
     }

     @GetMapping("/list")
    public R<List<BaseInfo>> getBaseInfoList(HttpServletRequest request)
     {
         String token = request.getHeader("token");
         Long id = JWTUtils.parseToken(token);
         QueryWrapper<BaseInfo> baseInfoQueryWrapper = new QueryWrapper<>();
         baseInfoQueryWrapper.eq("user_Id",id);
         List<BaseInfo> baseInfoList = baseInfoService.list(baseInfoQueryWrapper);
         return R.success(baseInfoList);

     }
}

